package com.right.health.app.ui.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.utils.VerifyUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.net.UserApi;
import com.right.health.app.utils.EnterVerifyUtil;

/*
 * 社保卡
 * Created by hopeliao on 2019-10-01
 *
 */
public class SocialSecurityCardActivity extends BaseActivity implements OnHttpListener {

    private static final int HTTP_CODE_ADD = 1;

    private EditText mNameEt, mPhoneEt, mIdcardEt;

    private UserApi mUserApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.social_security_card);
    }

    @Override
    public void findView() {
        mNameEt = (EditText) findViewById(R.id.et_name);
        mPhoneEt = (EditText) findViewById(R.id.et_phone);
        mIdcardEt = (EditText) findViewById(R.id.et_idcard);

        findViewById(R.id.btn_save).setOnClickListener(v -> send2Net());
    }

    @Override
    public void initialize() {
        mUserApi = new UserApi();
        mUserApi.setListener(this);
    }

    private void send2Net() {
        String name = mNameEt.getText().toString().trim();
        if(TextUtils.isEmpty(name)) {
            ToastUtil.show(this, mNameEt.getHint().toString());
            return;
        }
        String idcard = mIdcardEt.getText().toString().trim();
        if(TextUtils.isEmpty(idcard) || !EnterVerifyUtil.verifyCardid(idcard)) {
            ToastUtil.show(this, R.string.please_enter_id_card_number);
            return;
        }
        String phone = mPhoneEt.getText().toString().trim();
        if(TextUtils.isEmpty(phone) || !VerifyUtil.isMobile(phone)) {
            ToastUtil.show(this, mPhoneEt.getHint().toString());
            return;
        }

        mUserApi.addRequestCode(HTTP_CODE_ADD);
        mUserApi.addSocialSecurity(name, idcard, phone);
    }

    @Override
    public void onResponse(Request request, Response response) {
        if(response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            ToastUtil.show(this, "添加成功");
            sendBroadcast(BroadCastCode.REFRESH_COMMON);
            finish();
        }
    }
}
